<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" v-model.trim="inputValue" />
    <a-button type="primary" @click="addItemHandle">添加事项</a-button>

    <a-list bordered :dataSource="list" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="changeItemStatus({ id: item.id })">{{ item.info }}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="deleteItem({ id: item.id })">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{ undoneLength }}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button
            :type="keyList === 'all' ? 'primary' : ''"
            @click="changeList({ key: 'all' })"
          >全部</a-button>
          <a-button
            :type="keyList === 'undone' ? 'primary' : ''"
            @click="changeList({ key: 'undone' })"
          >未完成</a-button>
          <a-button
            :type="keyList === 'done' ? 'primary' : ''"
            @click="changeList({ key: 'done' })"
          >已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="cleanDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'

export default {
  name: 'app',
  data() {
    return {
      inputValue: ''
    }
  },
  computed: {
    ...mapState(['keyList']),
    ...mapGetters(['undoneLength', 'list'])
  },
  methods: {
    ...mapMutations([
      'addItem',
      'deleteItem',
      'changeItemStatus',
      'cleanDone',
      'changeList'
    ]),
    ...mapActions(['getList']),
    addItemHandle() {
      if (this.inputValue === '') {
        return this.$message.warning('不能为空')
      }
      this.addItem({ info: this.inputValue })
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
